package org.skywind.Index.HitMappers;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.skywind.Index.IndexManager;
import org.skywind.Robot.SpiderBase;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.skywind.Robot.pageRobots.ImageTagParser;
import org.skywind.WindSearch.Beans.PicBean;

public class MapPic extends AbstractMapHits {
	
	public List mapping(MapEvent me) {
		Hits hits = me.getHits();
		Query query=me.getQuery();
		IndexManager im = me.getIndex();
		SpiderBase sb = me.getSpiderbase();
		List list = new ArrayList();
		for (int i = 0; i < hits.length(); i++)
			try {
				Document doc = hits.doc(i);
				PicBean pic = new PicBean();
				pic.setTitle(highlight(query,ImageTagParser.TITLE,doc.get(ImageTagParser.TITLE), im.getAnalyzer()));
				pic.setUrl(doc.get(ImageTagParser.URL));
				pic.setTime(new Date(Long.parseLong(doc
						.get(ImageTagParser.TIME))));
				pic.setSize(Long.parseLong(doc.get(ImageTagParser.SIZE)));
				pic.setDim(new Dimension(Integer.parseInt(doc
						.get(ImageTagParser.WIDTH)), Integer.parseInt(doc
						.get(ImageTagParser.HEIGHT))));
				pic.setSourceid(hits.id(i));
				pic.setFileName(doc.get(ImageTagParser.FILENAME));
				pic.setType(doc.get(ImageTagParser.TYPE));
				pic.setRefer(doc.get(ImageTagParser.REFER));
				pic.setSourceIP(sb.getRobotManager().getDataHost());
				pic.setRank((double) i / hits.length());
				list.add(pic);
				if (maxResult >0 && i >= maxResult)
					break;
			} catch (Exception e) {
				e.printStackTrace();
			}
		return list;
	}

}
